﻿CREATE PROCEDURE [acms].[Template_Put]
	@Id int, 
	@SiteId int,
	@Name varchar(255),
	@Notes varchar(2000),
	@NodeType int,
	@ExcludeFromSearch bit,
	@VersionId timestamp,
	@IdOut int output,
	@VersionIdOut timestamp output
AS
	if Exists (Select null FROM Template_View with (nolock) where Id = @Id) begin
		update Templates
		set
			SiteId = @SiteId,
			Name = @Name,
			Notes = @Notes,
			NodeType = @NodeType,
			ExcludeFromSearch = @ExcludeFromSearch
		where Id = @Id
			and VersionId = @VersionId
		
		if(@@RowCount = 0)
			return -100;
		else
			set @IdOut = @Id
	end
	else if @Id is null or @Id = 0 begin
			insert	Templates
			(SiteId, Name, Notes, NodeType, ExcludeFromSearch)
		values
			(@SiteId, @Name, @Notes, @NodeType, @ExcludeFromSearch)
		set @IdOut = @@Identity
	end
	else begin
		return -200
	end
	select @VersionIdOut = VersionId From Templates with (nolock) where Id = @IdOut
RETURN 0;